<template>
  <div id="app">
    <div id="topwrap">
      <div id="topkong"></div>
      <!-- <div id="top"></div> -->
    </div>
    <div id="main">
      <img class="img" src="../../assets/logo2.png" alt="">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <!-- <el-form-item label="昵称" prop="nickname">
          <el-input type="password" v-model="ruleForm.nickname" autocomplete="off"></el-input>
        </el-form-item> -->

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div id="footer">浙ICP备2021001007号-1 杭州海拾一科技有限公司所有 Copyright © 2019-2021 版权所有.</div>
  </div>
</template>

<script>
import axios from "axios";
import { validateUser, validatePass } from "@/utils/validate"
import {Message} from "element-ui"
// import { validateUser, validatePass,validateNick} from "@/utils/validate"
import md5 from "md5";
// 密钥
import { secret } from "@/utils/secret"
export default {
  data() {
    // const validatePass=(_,value)=>{
    //   if(!value){
    //     return cancelIdleCallback(new Error("确认密码不为空"))
    //   }
    //   return callback();
    // }


    return {
      ruleForm: {
        username: "",
        password: "",
        // nickname:""
      },
      rules: {
        username: [{ validator: validateUser, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
        // nickname: [{ validator: validateNick, trigger: 'blur' }],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 调用登录
          // this.$store.dispatch("user/LOGIN_ACTION",this.ruleForm).then((res)=>{
          //   if(res){
          //     // 记录一下---加上redirect路径
          //     let path=this.$route.query.redirect||"/";
          //     setTimeout(()=>{
          //       this.$route.push(path);
          //     },2000)

          //   }
          //   console.log(res,"login");
          // })
          // 提示登录成功
          // alert('submit!');
          // 发送请求
          let res = await axios({
            url: "http://182.92.162.156:8083/users/register",
            method: "GET",
            // 加密
            params: {
              ...this.ruleForm,
              password: md5(this.ruleForm.password + secret)
            }
          });
          console.log(res, "注册");
          let path = this.$route.query.redirect || "/login";
          setTimeout(() => {
            this.$router.push(path);
            if (res.data.code == '1') {
              Message({
                message: '注册成功',
                type: "success"
              })
            }
            }, 1000);
        } else {
          console.log('注册失败，请重新注册!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>


<style scoped>
#main{
  position: relative;
}
#main>img {
  width: 200px;
  height: 150px;
  margin-left: 10px;
  position: absolute;
  left: 10px;
  top: 10px;
}

h1 {
  margin-left: 160px;
}

p {
  font-size: 12px;
  text-align: center;
  margin-bottom: 14px;
}

form {
  width: 423px;
  height: 444px;
  position: absolute;
  right: 200px;
  bottom: 100px;
}

#topwrap {
  display: flex;
}

#top {
  width: 100%;
  height: 60px;
  background: #fbd70b;
}

#topkong {
  width: 100px;
}

#main {
  width: 100%;
  height: 778px;
  margin: 0 auto;
  position: relative;
  background: url(../../assets/bjt1.png) no-repeat;
  background-size: 100% 778px;
}

#t2>input {
  width: 400px;
  height: 48px;
  border-radius: 40px;
  outline: none;
  border: none;
  text-indent: 2em;
}

#t3 {
  display: flex;
}

#t3>input {
  height: 48px;
  width: 200px;
  outline: none;
  border-radius: 40px;
  border: none;
  text-indent: 2em;
  margin-right: 14px;
}

#t4 {
  margin-top: 20px;
}

#t1,
#t2,
#t3,
#t4,
#t5 {
  margin-bottom: 30px;
}

#t3>button {
  background-color: #FDDA11;
  color: #000;
  border: none;
  width: 169px;
  height: 48px;
  border-radius: 50px;
  margin-left: 10px;
}

#t4>button {
  background-color: #FDDA11;
  color: #000;
  border: none;
  width: 169px;
  height: 48px;
  border-radius: 50px;
  margin-left: 130px;
}

#footer {
  width: 100%;
  height: 43px;
  text-align: center;
  line-height: 43px;
  color: #565353;
  color: snow;
  font-size: 12px;
  position: absolute;
  bottom: 0px;
  right: 40px;
}
</style>@/utils/secret